

$(document).ready(function(){
	bannerRollingSet(".top_ban",".imgs li",".btns .count em",".btns .count strong",".btns .btn_page .btn_next_plan",".btns .btn_page .btn_prev_plan", 1);

	var hasJsTab = $(".js_tab").length;
	if (hasJsTab) {
		$(".js_tab").children().hide();
		$(".js_tab").children().eq(0).show();

		$(".js_tab_bar").children().hover(function() {
			var tabNum = $(this).index();

			$(".js_tab").children().hide();
			$(".js_tab").children().eq(tabNum).show();

			return false;
		});
	}
	var hasJsTab2 = $(".js_tab2").length;
	if (hasJsTab2) {
		$(".js_tab2").children().hide();
		$(".js_tab2").children().eq(0).show();

		$(".js_tab_bar2").children().hover(function() {
			var tabNum = $(this).index();

			$(".js_tab2").children().hide();
			$(".js_tab2").children().eq(tabNum).show();

			return false;
		});
	}
	var hasJsTab3 = $(".js_tab3").length;
	if (hasJsTab3) {
		$(".js_tab3").children().hide();
		$(".js_tab3").children().eq(0).show();

		$(".js_tab_bar3").children().hover(function() {
			var tabNum = $(this).index();

			$(".js_tab3").children().hide();
			$(".js_tab3").children().eq(tabNum).show();

			return false;
		});
	}
	var hasJsTab4 = $(".js_tab4").length;
	if (hasJsTab4) {
		$(".js_tab4").children().hide();
		$(".js_tab4").children().eq(0).show();

		$(".js_tab_bar4").children().hover(function() {
			var tabNum = $(this).index();

			$(".js_tab4").children().hide();
			$(".js_tab4").children().eq(tabNum).show();

			return false;
		});
	}

});



//gnb
function GNB(){
	var o = jQuery(".ifa_gnb_wrap .dep1");
	var d1 = o.find(".d1");
	var d1A = o.find(".d1>a");
	var d2 = o.find(".dep2_wrap");
	var d2A = o.find(".dep2_wrap a");
	var d1A_on = d1A.filter(".on");
	var d2_on = d2.filter(".on");
	var d2A_on = d2A.filter(".on");
	var speed = 0;
	var timer;
	
	//처음 상태
	function reset(){
		o.css("zIndex",0);
		var el_dep1 = d1A_on.find("img");
		//2뎁스 활성화시 아래 주석 풀기
		//if(!d2A_on.parents(".depth2").eq(0).filter(":visible").length){
			menuOn(el_dep1);
			//menuOn(el_dep1,d2_on);
		//}
		//subOn(d2A_on);
	}
	
	//1depth off
	function menuOff(){
		var curr_dep2 = d2.filter(":visible");
		var curr_dep1 = curr_dep2.prev().find("img");
		d1.find(">.on img").chgImg(0); //2뎁스 활성화시 삭제
		curr_dep1.chgImg(0);
		curr_dep2.find("img").chgImg(0);
		d2.hide();
	}
	
	//1depth on
	function menuOn(el_dep1,el_dep2){
		menuOff();
		el_dep1.chgImg(1);
		if(typeof el_dep2 == "object"){
			el_dep2.show();
		}
	}
	
	//2depth on
	function subOn(el){
		el.parents(".dep2_wrap").eq(0).find("img").chgImg(0);
		//el.find("img").chgImg(1);
	}
	
	//활성화 상태인지 체크
	function chkCurr(el){
		if(el.filter(":visible").length){
			return true;
		}else{
			return false;
		}
	}
	
	//1depth mouseover
	d1A.each(function(i){
		jQuery(this).mouseover(function(){
			//clearTimeout(timer2);
			//all_wrap.hide();
			clearReset();
			o.css("zIndex",1);
			if(chkCurr(d2.eq(i))) return;
			var el_dep1 = jQuery(this).find("img");
			var el_dep2 = d2.eq(i);
			menuOn(el_dep1,el_dep2);
		});
	});
	
	//2depth mouseover
	d2A.mouseover(function(){
		clearReset();
		subOn(jQuery(this));
	});
	
	//처음 상태로 가기 취소
	function clearReset(){
		clearTimeout(timer);
	}
	
	//완전히 벗어나면 처음 상태로 가기
	o.mouseleave(function(){
		clearReset();
		timer = setTimeout(function(){
			reset();
		},200);
	});
	
	reset();
	
	//전체메뉴
	/*var all_btn = jQuery("#gnb .allMenu img");
	var all_wrap = jQuery("#gnb .type2");
	var all_d2 = all_wrap.find(".depth2 a");
	
	var timer2;
	all_btn.bind("mouseover",function(){
		clearTimeout(timer);
		clearTimeout(timer2);
		menuOff();
		all_wrap.show();
	})
	all_btn.mouseleave(function(){
		timer2 = setTimeout(function(){
			reset();
			all_wrap.hide();
		},200);
	});
	all_wrap.mouseover(function(){
		menuOff();
		clearTimeout(timer);
		clearTimeout(timer2);
	})
	.mouseleave(function(){
		timer2 = setTimeout(function(){
			reset();
			all_wrap.hide();
		},200);
	});
	
	all_d2.hover(function(){
		var image = jQuery(this).find("img");
		var imgsrc = image.attr("src");
		var on = imgsrc.replace(/.gif$/gi,"_on.gif");
		image.attr("src",on);
	},function(){
		var image = jQuery(this).find("img");
		var imgsrc = image.attr("src");
		var on = imgsrc.replace(/_on.gif$/gi,".gif");
		image.attr("src",on);
	});*/
}

//현재 페이지에 on class
function setTopNaviCurrentDepth(){
	if(typeof getCurrentDepth != "function"){
		GNB();
		return;
	}
	var currentDepth = String(getCurrentDepth());
	var depth1 = parseInt(currentDepth.substr(0,2))-1;
	var depth2 = parseInt(currentDepth.substr(2,2))-1;
	
	if(depth1>=0){
		var o = jQuery(".ifa_gnb_wrap .dep1");
		var d1A = o.find(".d1>a").eq(depth1);
		var d2 = o.find(".d1").eq(depth1).find(".dep2_wrap");
		var d2A = d2.find("a").eq(depth2);
		d1A.addClass("on");
		d2.addClass("on");
		d2A.addClass("on");
	}
	
	GNB();
}

function SNB(){
	var o = jQuery("#snb .depth1:visible");
	var d2 = o.find(".depth2 li:not(.on)");
	var d2_curr = o.find(".depth2 li.on");
	function def(){
		d2_curr.removeClass("on").find("img").chgImg(0);
	}
	function reset(){
		d2_curr.addClass("on").find("img").chgImg(1);
	}
	d2.hover(function(){
		def();
		jQuery(this).addClass("on").find("img").chgImg(1);
	},function(){
		jQuery(this).removeClass("on").find("img").chgImg(0);
		reset();
	});
	reset();
}

//현재 페이지에 on class
function setLeftNaviCurrentDepth(){
	if(typeof getCurrentDepth != "function"){
		return;
	}
	var currentDepth = String(getCurrentDepth());
	var depth2 = parseInt(currentDepth.substr(2,2))-1;
	var o = jQuery("#snb .depth1");
	var d2 = o.find(".depth2");
	d2 = d2.find("li").eq(depth2);
	if(depth2>=0){
		//console.log(d2.find("a").attr("href"));
		d2.addClass("on");
	}
	SNB();
}

jQuery(window).load(function(){
	setTopNaviCurrentDepth();
	setLeftNaviCurrentDepth();
	indexRollBanner();
});


//이미지 over
jQuery.fn.chgImg = function(on,options){
	jQuery(this).each(function(){
		if(this.tagName != "IMG") return;
		var imgSrc = jQuery(this).attr("src");
		var settings = {
			imgType : imgSrc.match(/gif$|jpg$|png$/),
			replaceTxt : "_on"
		}
		settings = jQuery.extend(settings, options || {});
		settings.imgType = "." + settings.imgType;
		settings.replaceTxt = settings.replaceTxt+settings.imgType;
		
		var chkOn = imgSrc.indexOf(settings.replaceTxt) != -1;
		if(on){
			if(chkOn){
				//jQuery(this).unbind("mouseout");
				return;
			}else{
				jQuery(this).attr("src",imgSrc.replace(settings.imgType,settings.replaceTxt));
			}
		}else if(chkOn){
			jQuery(this).attr("src",imgSrc.replace(settings.replaceTxt,settings.imgType));
		}
	});
}




// indexRollBanner
function indexRollBanner() {

	var $rollWrap = $(".js_rollBanner .imgs"),
		$item = $rollWrap.children(".item"),
		itemLength = $item.length,
		nowIdx = 0,
		lastIdx = itemLength - 1,
		timer,
		rollDelay = 2000;

	//$("body").fadeIn(1200);

	// pager Create
	var $pager = $(".js_rollBanner .btns li")

	

	var $pagerNum = $pager.children("a");

	$pagerNum.first().addClass("on");

	// setInterval
	timer = setInterval(rolling, rollDelay);

	// interval Stopping
	$rollWrap.bind({
		mouseenter : function() {
			clearInterval(timer);
		},
		mouseleave : function() {
			if ( timer ) timer;
			timer = setInterval(rolling, rollDelay);
		}
	});

	// pager Click
	$pagerNum.each(function(i) {
		$(this).bind({
			click : function() {
				nowIdx = i;
				changeVisual(nowIdx);
				pager(nowIdx);
			}
		});
	});

	// rolling
	function rolling() {

		nowIdx += 1;

		if ( nowIdx == itemLength ) nowIdx = 0;

		changeVisual(nowIdx);
		pager(nowIdx);

	}

	// changeVisual
	function changeVisual(idx) {
		$item.hide();
		$item.eq(idx).show();
	}

	// pager Check
	function pager(idx) {
		$pagerNum.removeClass("on");
		$pagerNum.eq(idx).addClass("on");
	}


}

/************************************************
func : banner rolling Script
*************************************************/
var ta = new Array();
var cnta = new Array();
var oba = new Array();
var obna = new Array();
function bannerRollingSet(el,content,allNumber,onNumber,nextBtn,prevBtn,i){
	var content = $(el+">"+content);
	var allNumber = $(el+">"+allNumber);
	var onNumber = $(el+">"+onNumber);
	var nextBtn = $(el+">"+nextBtn);
	var prevBtn = $(el+">"+prevBtn);
	oba[i] = content;
	obna[i] = onNumber;
	cnta[i] = 0;
	$(content+":first").show();
	$(allNumber).text($(content).size());
	$(onNumber).text($(content).index()+1);
	$(prevBtn).click(function(){
		cnta[i] = cnta[i] - 1;
		if (cnta[i] <= 0) cnta[i] = $(content).size();
		$(content).hide().eq(cnta[i]-1).show();
		$(onNumber).text(cnta[i]);
	}).mouseenter(function(){
		$(this).addClass('over');
	}).mouseleave(function(){
		$(this).removeClass('over');
	});
	$(nextBtn).click(function(){
		if (cnta[i] > $(content).size()-1) cnta[i] = 0;
		$(content).hide().eq(cnta[i]).show();
		cnta[i] = cnta[i] + 1;
		$(onNumber).text(cnta[i]);
	}).mouseenter(function(){
		$(this).addClass('over');
	}).mouseleave(function(){
		$(this).removeClass('over');
	});
	$(content,nextBtn,prevBtn).mouseenter(function(){clearBanner3(i);cnta[i] = cnta[i] - 1;}).mouseleave(function(){bannerRolling(i);});
	bannerRolling(i);
}
function bannerRolling(i){
	var content = oba[i];
	var onNumber = obna[i];
	if (cnta[i] > $(content).size()-1) cnta[i] = 0;
	$(content).hide().eq(cnta[i]).show();
	cnta[i] = cnta[i] + 1;
	$(onNumber).text(cnta[i]);
	ta[i]=setTimeout(function(){bannerRolling(i);}, 3000);
}
function clearBanner3(i){
	clearTimeout(ta[i]);
}